/*
 * Copyright (c) 2009, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.wso2.carbon.bpel.b4p.internal;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.axis2.context.ConfigurationContext;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;
import org.wso2.carbon.utils.ConfigurationContextService;
import org.wso2.carbon.utils.Utils;
import org.wso2.carbon.registry.core.service.RegistryService;
import org.wso2.carbon.datasource.DataSourceInformationRepositoryService;

/**
 * @scr.component name="org.wso2.carbon.bpel.B4PServiceComponent" immediate="true"
 * @scr.reference name="org.wso2.carbon.configCtx"
 * interface="org.wso2.carbon.utils.ConfigurationContextService" cardinality="1..1"
 * policy="dynamic" bind="setConfigurationContext" unbind="unsetConfigurationContext"
 * @scr.reference name="datasource.information.repository.service"
 * interface="org.wso2.carbon.datasource.DataSourceInformationRepositoryService"
 * cardinality="1..1" policy="dynamic"  bind="setDataSourceInformationRepositoryService"
 * unbind="unsetDataSourceInformationRepositoryService"
 * @scr.reference name="registry.service" interface="org.wso2.carbon.registry.core.service.RegistryService"
 * cardinality="1..1" policy="dynamic"  bind="setRegistryService" unbind="unsetRegistryService"
 */

public class B4PServiceComponent {
    private static Log log = LogFactory.getLog(B4PServiceComponent.class);
    private BundleContext bundleContext;
    private static ConfigurationContext configCtx;
    private static ConfigurationContext clientConfigCtx;
    private static ConfigurationContextService configCtxService = null;
    private boolean dataSourceInfoRepoProvided = false;
    private RegistryService registryService;
    /* For the moment it's look like we don't want multithreaded http connection manager*/
    private static MultiThreadedHttpConnectionManager httpConnectionManager;

    public static MultiThreadedHttpConnectionManager getHttpConnectionManager() {
        return httpConnectionManager;
    }

    protected void activate(ComponentContext ctxt) {
        try {
            this.bundleContext = ctxt.getBundleContext();
            if (configCtx != null && dataSourceInfoRepoProvided) {
                Utils.registerDeployerServices(this.bundleContext);
            }
            //TODO these params should taken frm config file OR DO we even need to set these
            httpConnectionManager = new MultiThreadedHttpConnectionManager();
            httpConnectionManager.getParams().setDefaultMaxConnectionsPerHost(2);
            httpConnectionManager.getParams().setMaxTotalConnections(20);
        } catch (Throwable t) {
            log.error("Failed to activate the BPELServiceComponent", t);
        }
        if(log.isDebugEnabled())
            log.debug("BPEL Deployer bundle is activated.");
    }


    protected static void setConfigurationContext(ConfigurationContextService configCtxSvc) {
        configCtxService = configCtxSvc;
        configCtx = configCtxSvc.getServerConfigContext();
        clientConfigCtx = configCtxSvc.getClientConfigContext();
    }

    public static ConfigurationContext getConfigurationContext() {
        return configCtx;
    }

    public static ConfigurationContext getClientConfigurationContext(){
        return clientConfigCtx;
    }

    public static ConfigurationContextService getConfigurationContextService() {
        return configCtxService;
    }


    protected static void unsetConfigurationContext(ConfigurationContextService configCtxSvc) {
        configCtxService = null;
        configCtx = null;
        clientConfigCtx = null;
    }

    protected void setDataSourceInformationRepositoryService(
            DataSourceInformationRepositoryService repositoryService) {
        this.dataSourceInfoRepoProvided = true;
    }

    protected void unsetDataSourceInformationRepositoryService(
            DataSourceInformationRepositoryService repositoryService) {
        this.dataSourceInfoRepoProvided = false;
    }

    protected void setRegistryService(RegistryService registryService) {
        if (log.isDebugEnabled()) {
            log.info("Setting the Registry Service");
        }
        this.registryService = registryService;
    }

    protected void unsetRegistryService(RegistryService registryService) {
        if (log.isDebugEnabled()) {
            log.info("Unsetting the Registry Service");
        }
        this.registryService = null;
    }

}
